package com.wangshaoyu.controller;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.wangshaoyu.fallback.FallBackResource;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class TestController {

    @Autowired
    private DemoClient demoClient;

    @GetMapping("/getFegin")
    @SentinelResource(value = "getFeginResource",blockHandler = "getFeginResourceBlockHandler",blockHandlerClass = TestController.class)
    public Object getFegin() {
        return demoClient.getFeigen();
    }
    @GetMapping("/test")
    @SentinelResource(value = "testGetResource",blockHandler = "testGetResourceBlockHandler")
    public String test01(){
        return "test";
    }
    public static Object getFeginResourceBlockHandler(BlockException blockException){
        return "被限流了";
    }
    public String testGetResourceBlockHandler(BlockException blockException){
        return "被限流了";
    }

    @GetMapping("/stopService")
    @SentinelResource(value = "stopServiceResource",fallback = "stopServiceFallback",fallbackClass = FallBackResource.class)
    public String stopService(){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return "服务正常使用";
    }

    @GetMapping("/hotSpot")
    @SentinelResource(value = "hotSpotResource",blockHandler = "hotSpotResourceBlockHandler")
    public String hotSpot(@RequestParam(value = "userId",required = false)String userId,
                          @RequestParam(value = "username",required = false) String username){
        return "热点数据";
    }
    public String hotSpotResourceBlockHandler(String userId,String username,BlockException blockException){
        return "被限流了";
    }

    @GetMapping("/commonService")
    public String commonService(){
        return "下游服务";
    }

    @GetMapping("/eA")
    public String eA(){
        return commonService();
    }
    @GetMapping("/eB")
    public String eB(){
        return commonService();
    }
    public String commonServiceFallback(BlockException blockException){
        return "被限流了";
    }
}
